package me.xuym.crawler.dygang.loader;

import com.cbs.java.component.application.Application;
import com.cbs.java.component.network.HttpClient;
import com.cbs.java.component.network.Request;
import com.cbs.java.component.network.Response;
import com.cbs.java.component.network.decoder.TextDecoder;
import me.xuym.crawler.Processor;
import me.xuym.crawler.dygang.entity.DYGangDBResult;
import me.xuym.crawler.dygang.entity.DYGangDownloadResult;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by broche on 13/09/2017.
 */
public class DYGangWebDetailLoader extends Processor<List<DYGangDBResult>, List<DYGangDownloadResult>> {
    private HttpClient httpClient = new HttpClient.Builder().build();

    public DYGangWebDetailLoader(Application application) {
        super(application);
    }

    @Override
    protected List<DYGangDownloadResult> onProcess(List<DYGangDBResult> dyGangDBResults) throws Exception {
        List<DYGangDownloadResult> results = new ArrayList<>();
        for (DYGangDBResult result : dyGangDBResults) {
            Request request = new Request(result.getUrl()).setDecoder(new TextDecoder("gbk"));
            Response response = httpClient.sendSync(request);
            String content = (String) response.getResult();
            content = content.replace("\n", "");
            Pattern listPattern = Pattern.compile("<table border=\"0\" cellspacing=\"1\" cellpadding=\"10\" width=\"100%\" bgcolor=\"#0099cc\">(.*?)</table>");
            Matcher listMatcher = listPattern.matcher(content);
            if (listMatcher.find()) {
                Pattern itemPattern = Pattern.compile("<td[^>]*?>([^<]*?)<a href=\"([^\"]*?)\"[^>]*?>([^<]*?)</a>([^<]*?)</td>");
                Matcher itemMatcher = itemPattern.matcher(listMatcher.group(1));
                while (itemMatcher.find()) {
                    DYGangDownloadResult tresult = new DYGangDownloadResult();
                    tresult.setDyid(result.getId());
                    String type = cleanString2(itemMatcher.group(1));
                    tresult.setType(type.equals("") ? "未知" : type);
                    String url = itemMatcher.group(2);
                    tresult.setUrl(url.equals("") ? "未知" : url);
                    String name = cleanString(itemMatcher.group(3));
                    tresult.setName(name.equals("") ? "未知" : name);
                    String size = cleanString2(itemMatcher.group(4));
                    tresult.setSize(size.equals("") ? "未知" : size);
                    results.add(tresult);
                }
            }
        }
        return results;
    }

    private String cleanString(String str) {
        return str.replace("&nbsp;", "").trim();
    }

    private String cleanString2(String str) {
        return cleanString(str.replace(":", "")
                .replace("：", "")
                .replace("(", "")
                .replace("（", "")
                .replace(")", "")
                .replace("）", "").trim());
    }
}
